// pages/goods_set_class/index.js
import {
    getNavigationbar,
    ReShopProduct,
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        scrollViewHeight: 0,
        goodsList: [
        ],
        isClass: false,
        categoryList: [],
    },
    handleItemClick(e) {
        console.log('点击', e)
        const selectedItem = e.currentTarget.dataset.item;
        this.data.categoryList.forEach(element => {
            element.child.forEach(elementj => {
                if (elementj.id === selectedItem.id) {
                    elementj.selected = true;
                    elementj.type = selectedItem.name
                } else {
                    elementj.selected = false;
                }
            });
        });
        this.data.goodsList.forEach(element => {
            if (element.check) {
                element.type = selectedItem.name;
                element.scatid = selectedItem.id;
                element.bcatid = selectedItem.pid;
            }
        });
        console.log('this.data.categoryList', this.data.goodsList)
        this.setData({
            categoryList: this.data.categoryList, // 更新数据
            isClass: false,
            goodsList: this.data.goodsList
        });
    },
    handNextstep() {
        console.log('handNextstep')
        let selectedGoods = this.data.goodsList.filter(item => item.check); // 过滤出被选中的商品
        if (selectedGoods.length === 0) {
            wx.showToast({
                title: '请先选择商品',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        if (!this.data.categoryList.some(category => category.child.some(item => item.selected))) {
            wx.showToast({
                title: '请先选择商品分类',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        wx.navigateTo({
            url: '/subpackages/pages/Warehousing/index?data=' + JSON.stringify(selectedGoods),
        });
        console.log('this.data.goodsList',selectedGoods)
    },
    gethandle() {
       

    },
    /**
     * 选择商品回调函数方法
     * @param {*} e 
     */
    handleSelCheck(e) {
        const {
            index
        } = e.currentTarget.dataset
        let goodsList = this.data.goodsList // 获取购物车数据
        if (goodsList[index].check) {
            goodsList[index].check = false
        } else {
            goodsList[index].check = true
        }
        // 重新赋值 渲染购物车数据
        this.setData({
            goodsList: goodsList
        })
    },

    /**
     * 设置分类回调函数方法
     */
    handleSetClass() {
        this.setData({
            isClass: true
        })
        ReShopProduct({
            act: 'category_list',
            p: 1,
            rollpage: 10000,

        }).then(res => {
            
            console.log('商品分类列表', res)
            this.setData({
                categoryList: res
            })
        })
    },
    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
        let that = this
        let query = wx.createSelectorQuery().in(this)
        query.select('.header').boundingClientRect()
        query.select('.footer').boundingClientRect()
        query.exec(res => {
            let headerHeight = res[0].height;
            let footerHeight = res[1].height;
            let winHeight; // 窗口可使用高度
            wx.getSystemInfo({
                success: (res) => {
                    winHeight = res.windowHeight
                }
            })
            let scrollHeight = winHeight - headerHeight - footerHeight; // 滚动区域高度
            that.setData({
                scrollViewHeight: scrollHeight
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.data) {
            let params = JSON.parse(options.data)
            this.data.goodsList = [...params]
            this.data.goodsList.forEach(element => {
                Object.assign(element, {
                    type: '未设置'
                })
                element.check = false
            });
            console.log('进', params, this.data.goodsList)

            this.setData({
                goodsList: params,
            })
            this.computeScrollViewHeight();

        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})